<?php include('header.php');?>

<div class="row-fluid">
	<div class="span9" id="heightContenu">
		<div class="tab-content">
			<div class="tab-pane active" id="product_info">
			
			<?php echo form_open($this->config->item('admin_folder').'/newsletter' ); ?>
			<?php echo validation_errors(); ?>
				<div class="row-fluid">
						<?php
						$data	= array('placeholder'=>'Email listes','name'=>'emails', 'class'=>'row-fluid', 'rows'=>2, 'id'=>'emails');
						echo form_textarea($data);
						?>
				</div>
			
				<div class="row-fluid">
						<?php echo form_error('subjet'); ?>
						<?php
						$data	= array('placeholder'=>'Subjet', 'name'=>'subjet', 'value'=>set_value('subjet', $subjet), 'class'=>'span10','id'=>'Titre');
						echo form_input($data);
						?>
				</div>
				
				<div class="row-fluid">
						<?php echo form_error('description'); ?>
						<?php
						
						$data	= array('name'=>'description','rows'=>10, 'class'=>'redactor', 'value'=>set_value('description', $description));
						echo form_textarea($data);
						
						?>
				</div>
				
				<div class="form-actions">
					<input type="submit" class="btn btn-primary" name="save" value="<?php echo lang('form_save');?>"/>
					<button class="btn btn-primary" onclick="sendMail()"  type="button">Envoyer</button>
					<input type="submit" class="btn btn-inverse" name="reset" value="Reset"/>
					<button class="btn btn-danger" onclick="sendAllMail()"  type="button">Envoyer automatiquement</button>
				</div>
				<?php echo form_close();?>
				
			</div>
		</div>
		<div id="send_iframe" class="row-fluid" style="height: 100%"  height="100%">
		</div>
	</div>
	
	
	<div class="span3" id="heightEmaiList">
		<h3>Email Liste</h3>
		<button class="btn" onclick="sendEmailId()"><i class="icon-arrow-left"></i> Ajouter à la liste</button>
		<div id="emailToSend">
		<div class="row">
				<?php echo $this->pagination->create_links();?>	
		</div>
		<?php 
		if(isset($emails) and !empty($emails)):
		?>
		<?php foreach ($emails as $email):?>
			<label class="checkbox">
	            <input type="checkbox" checked="checked" value="<?php echo $email->email?>">
	            <?php echo $email->email?>
			</label>
		
		<?php endforeach;?>
		
		<div class="row">
				<?php echo $this->pagination->create_links();?>	
		</div>
		</div>	
		<?php endif;?>
		<hr/>
		<?php echo form_open($this->config->item('admin_folder').'/AddEmail'); ?>
		
			<?php
						
				$data	= array('name'=>'emailsliste', 'placeholder'=>'Ajouter emails; Séparés par ;');
				echo form_textarea($data);
						
			?>
			
				<div class="form-actions">
					<input type="submit" class="btn btn-primary" name="addEmail" value="Ajouter emails"/>
				</div>
		<?php echo form_close()?>
	</div>
	<script type="text/javascript">
		function page($page){
			$.get('<?php echo $admin_url?>newsletter/pageEmails'+$page, function(data) {
				$('#emailToSend').html(data);
			});
		}

		function sendEmailId(){
			listEmail='';
			$( ".checkbox input:checked" ).each(function( index ) {
				listEmail+=$(this).val()+';';
			});
			$('#emails').val(listEmail);
		} 

		function sendMail(){
			listEmail=$('#emails').val();
			if(listEmail!=''){
				var Emails = listEmail.split(';');
				 $.each(Emails, function(i,email) {
					 $.post("<?php echo $admin_url?>newsletter/send", { "emails": email },
						function(data){
			        		$('#send_iframe').append(data);
						});
				});
			}
			$('#emails').val('');
		} 
		function sendAllMail(){
			listEmail='';
			$.get('<?php echo $admin_url?>newsletter/allEmails', function(data) {
				var listEmail=data;
			});
			if(listEmail!=''){
				var Emails = listEmail.split(';');
				 $.each(Emails, function(i,email) {
					 $.post("<?php echo $admin_url?>newsletter/send", { "emails": email },
						function(data){
			        		$('#send_iframe').append(data);
						});
				});
			}
			$('#emails').val('');
		} 
		$(document).ready(function() {
			$('#heightContenu').height($('#heightEmaiList').height());
		});
	</script>
</div>

<?php include('footer.php'); ?>